package com.bwie.bwedu.fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;

import com.bwie.bwedu.R;
import com.bwie.bwedu.activity.MainActivity;
import com.bwie.bwedu.adapter.WaittingConfirmFragmentAdapter;
import com.bwie.bwedu.bean.Student;
import com.bwie.bwedu.bean.response.MainInfoResponse;
import com.bwie.bwedu.presenter.WaitingConfrimFragmentPresenterImpl;
import com.bwie.bwedu.ui.dialog.CustomBaseDialog;
import com.bwie.bwedu.utils.LogUtils;
import com.bwie.bwedu.view.InvalidView;
import com.bwie.bwedu.view.WaittingView;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/7/21.
 */
public class WaittingConfirmFragment extends BaseFragment implements WaittingView,WaittingConfirmFragmentAdapter.ItemClickListener,InvalidView {
    private View view;
    private PullToRefreshListView listView;
    private MainInfoResponse userResponse;
    private int pn=1;
    private WaitingConfrimFragmentPresenterImpl presenter;
    List<Student> list;
    private WaittingConfirmFragmentAdapter adapter;
    private CustomBaseDialog dialog;
    public int pos;
    @Override
    public View getView(LayoutInflater inflater) {
        view = inflater.inflate(R.layout.fragment_waittingconfirm,null);
        WaitingConfrimFragmentPresenterImpl  present=new WaitingConfrimFragmentPresenterImpl(this);
        list=new ArrayList<Student>();
        return view;
    }
    @Override
    public void findView() {
        listView= (PullToRefreshListView) view.findViewById(R.id.listview);
//同时支持上拉下拉
        listView.setMode(PullToRefreshBase.Mode.BOTH);
    }
    @Override
    public void setData() {
        LogUtils.d("-------------ppppp------------------------");
        presenter = new WaitingConfrimFragmentPresenterImpl(this);
        presenter.presenter(0+"",pn+"");
    }
    //为PullToRefreshBase设置监听
    @Override
    public void setListener() {
        listView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
           //下拉刷新
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                pn=1;
                adapter.ThreadState();
                presenter.presenter(0+"",pn+"");
            }
//上拉加载
            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                pn++;
                adapter.ThreadState();
                presenter.presenter(0+"",pn+"");
            }
        });
    }
    @Override
    public void onSuccess(MainInfoResponse userResponse) {
//        if (list!=null)
//            list.clear();
        if(adapter==null){
            list.addAll(userResponse.list);
            adapter = new WaittingConfirmFragmentAdapter(getActivity(),list);
            listView.setAdapter(adapter);
        }
        else if(pn==1){
            list.clear();
//            LogUtils.d(userResponse.list.size()+"------------------------WaittingConfirmFragment----------------------------------");
            //对象不为空判断
            if(userResponse!=null&&userResponse.list!=null&&userResponse.list.size()!=0){
                LogUtils.d(userResponse.list.size()+"------------------------WaittingConfirmFragment-----------");
                list.addAll(userResponse.list);
            }
            adapter.notifyDataSetChanged();
            //更新完数据，加载停止
            listView.onRefreshComplete();
        }
        if(pn!=1){
//            list.clear();
            if(userResponse!=null&&userResponse.list!=null&&userResponse.list.size()!=0){
                showToast(userResponse+"========================");
                list.addAll(userResponse.list);
            }
            adapter.notifyDataSetChanged();
            //更新完数据，加载停止
            listView.onRefreshComplete();
        }
        adapter.setItemClickListener(this);
    }
    public  void getTime(int position){
        list.remove(position);
        adapter.notifyDataSetChanged();
    }
    @Override
    public void onFail(String info) {
        showToast(info);
    }
    //无效反馈方法
    @Override
    public void onInvalidClick(final int position) {
//        1.弹出对话框
        dialog = new CustomBaseDialog(getActivity());
        dialog.show();
        dialog.setText("是否将该信息返回市场部");
        dialog.setListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.cancel();
                pos=position;
                presenter.presenter(list.get(position).id);
            }
        });
    }
//    onValidClick确认有效监听
    @Override
    public void onValidClick(final int position) {

        //确认有效方法
//        1.弹出对话框
        dialog = new CustomBaseDialog(getActivity());
        dialog.show();
        dialog.setText("是否接受该学员并进行跟进工作");
        dialog.setListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pos=position;
                dialog.cancel();
                presenter.valid(list.get(pos).id);
                // TODO 写入presenter，成功后删除原数据
//                RequestAPI.valid(list.get(pos).id, new Response.Listener<BaseResponse>() {
//                    @Override
//                    public void onResponse(BaseResponse baseResponse) {
//                        showToast("待确认success");
//                    }
//                }, new Response.ErrorListener() {
//                    @Override
//                    public void onErrorResponse(VolleyError volleyError) {
//                        showToast("待确认fail");
//                    }
//                });
                //                Intent intent=new Intent(getActivity(), StudentInfoActivity.class);
//                intent.putExtra("student",student);
//                startActivity(intent);

            }
        });
    }
    //无效反馈确认按钮请求成功返回
    @Override
    public void InvalidSuccess(final MainInfoResponse userResponse) {
        if(userResponse.ok==true){
            ((MainActivity)getActivity()).addList(list.get(pos),3);
            list.remove(pos);
            adapter.notifyDataSetChanged();
//            dialog.cancel();
        }
    }
    //无效反馈确认按钮请求失败返回
    @Override
    public void InvalidError(String info) {
        showToast(info);
    }

    @Override
    public void validSuccess() {
        ((MainActivity)getActivity()).addList(list.get(pos),1);
        list.remove(pos);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void validFail(String info) {
        showToast(info+"");
    }
}
